package cn.jiedh.app.meiweidk.web;

import android.graphics.Bitmap;

import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

/**
 * Created by Administrator on 2015/4/28.
 */
public class DefaultWebViewClient extends WebViewClient {
    private boolean mHasStared;
    private boolean mClearHistory;

    public DefaultWebViewClient() {

    }

    public DefaultWebViewClient(DefaultWebViewClient client) {
        mHasStared = client.mHasStared;
        mClearHistory = client.mClearHistory;
    }

    public void setClearHistory(boolean clearHistory) {
        mClearHistory = clearHistory;
    }

    public void reset(DefaultWebViewClient client) {
        if (client != null) {
            mHasStared = client.mHasStared;
            mClearHistory = client.mClearHistory;
        } else {
            mHasStared = false;
            mClearHistory = false;
        }
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        CommonWebView webView = (CommonWebView) view;
        if (!mHasStared) {
            mHasStared = true;
            if (webView.isShowError()) {
//                try {
//                    webView.clearView();
//                } catch (Exception e) {
//
//                }
                webView.hideErrorView();
            }
        }
        webView.startLoadingProgress();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        mHasStared = false;
        ((CommonWebView) view).stopLoadingProgress();
        if (mClearHistory) {
            mClearHistory = false;
            view.clearHistory();
        }
    }

    @Override
    public void onLoadResource(WebView webView, String s) {
        super.onLoadResource(webView, s);
        if (mClearHistory) {
            webView.clearHistory();
        }
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        CommonWebView webView = (CommonWebView) view;
        webView.showErrorView();
        super.onReceivedError(view, errorCode, description, failingUrl);
        webView.stopLoadingProgress();
    }

//    public static void loadErrorPage(WebView view) {
//        String errorPage = FileHelper.readAssetFileAsString("error.html", view.getContext());
//        view.loadData(errorPage, "text/html", "UTF-8");
//        //view.loadDataWithBaseURL("file:///android_asset/", errorPage, "text/html", "UTF-8", null);
//    }
}
